<?php 
namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use think\Db;

class OrderController extends AdminBaseController
{

	/*园区房间列表页*/	
	public function index(){
		// var_dump($_POST);
		// EXIT;
		$wheret=array();
		$status= $this->request->param('status',2);	
		//echo $status;
		// var_dump($_POST);
		// exit;	
		$where['status']=1;
		/**搜索条件**/
		$park_name = $this->request->param('park_name');		
		$customer_name = $this->request->param('customer_name');
		$space_name = $this->request->param('space_name');
		$salesname = $this->request->param('salesname');
		$stime = $this->request->param('start_time','');
		$etime = $this->request->param('end_time','');
		$etime = $this->request->param('end_time','');
		$lease_status = $this->request->param('lease_status',10);
		$commpany_name = $this->request->param('commpany_name','');	
		$order_num=$this->request->param('order_num','');
		//按照孵化器名称搜索
		$condition='';
		if(!empty($park_name)  ){
		   	session('park_name',$park_name);
		}
		if(empty($park_name) && empty($_GET['page'])){
			session('park_name',null);
		}
		if(session('park_name')){
			//var_dump(session('park_name'));
			$park_name=session('park_name');
			$where_i['incubator_name'] = ['like' , "%$park_name%"];
			//var_dump($where_i['incubator_name']);
			$res=db('incubator')->where($where_i)->find();
			//var_dump($res);
		  	$condition['incubator_id'] =$res['id'];
		  	//var_dump($condition['incubator_id']);
		}	
		// if ($lease_status !='10') {
			
		// 	  $where['lease_status'] =$lease_status;
		
			
		// }
		// if ($commpany_name) {
		// 	$where['commpany_name'] = ['like', "%$commpany_name%"];
		// }
		if ($stime) {
			$where['stime'] = array('egt',strtotime($stime));
		}
        if ($etime) {
        	$where['etime'] = array('elt',strtotime($etime));
        }
		if ($salesname) {
			$where['salesman'] = ['like', "%$salesname%"];
		}
		// if ($customer_name) {
		// 	$where['customer_name'] = ['like', "%$customer_name%"];
		// }

		// if ($space_name) {
		// 	$where['space_name'] = ['like', "%$space_name%"];
		// }
		// var_dump($where);
		// exit;
		if(session('incubator_id')){
			$where['incubator_id']=session('incubator_id');
		}
		if(session('role_id')==3){
			$where['sales_id']=session('ADMIN_ID');
		}
		
		$list=Db::name('spaceOrder')->where($where)->where($wheret)->paginate(5);
		$now_time=date('Y-m-d');
		$now_time=time();
		$now_time2=date('Y-m-d H:i:s');
		//var_dump($where);
		foreach ($list as $key => $value) {

			
			$result=db('rent')->field('cycle_time,stime,etime')->where(array('order_id'=>$value['id'],'status'=>2))->find();
			//var_dump($result);
			if(!$result){
				$results=db('rent')->field('cycle_time')->where(array('order_id'=>$value['id']))->order('id desc')->find();
			}
			if($result){
				$rent_time=explode('---', $result['cycle_time']);
				if($now_time>=$result['stime'] && $now_time <=$result['etime']){
					db('spaceOrder')->where('id',$value['id'])->setField('current_price_status',1);
				}else{
				    db('spaceOrder')->where('id',$value['id'])->setField('current_price_status',0);
				}
			}else{
				db('spaceOrder')->where('id',$value['id'])->setField('current_price_status',0);	
			}
		}

			
		    /////////////////////////////////////////
			if(!empty($order_num)  ){
		   	   session('order_num',$order_num);
		    }
		    if(empty($order_num) && empty($_GET['page'])){
		   	   session('order_num',null);
		    }
		  	if(session('order_num')){
		  		$condition['order_num'] =['like','%'.session('order_num').'%'];
		  	}
			//////////////////////////////////////////
		  	if(!empty($space_name)  ){
		   	   session('space_name',$space_name);
		    }
		    if(empty($space_name) && empty($_GET['page'])){
		   	   session('space_name',null);
		    }
		  	if(session('space_name')){
		  		$condition['space_name'] =['like','%'.session('space_name').'%'];
		  	}
			/////////////////////////////////////////
		    if(!empty($customer_name)  ){
		   	   session('customer_name',$customer_name);
		    }
		    if(empty($customer_name) && empty($_GET['page'])){
		   	   session('customer_name',null);
		    }
		  	if(session('customer_name')){
		  		$condition['customer_name'] =['like','%'.session('customer_name').'%'];
		  	}
		  	/////////////////////////////////////////
		  	if($lease_status!=10){
		   	   session('lease_status',$lease_status);
		    }
		    if(empty($lease_status) && empty($_GET['page'])){
		   	   session('lease_status',null);
		    }
		  	if(session('lease_status')){
		  		$condition['lease_status'] = session('lease_status');
		  	}
		  	////////////////////////////////////////
		  	if(!empty($salesman)  ){
		   	   session('salesman',$salesman);
		    }
		    if(empty($salesman) && empty($_GET['page'])){
		   	   session('salesman',null);
		    }
		  	if(session('salesman')){
		  		$condition['salesman'] =['like','%'.session('salesman').'%'];
		  	}
		  	/////////////////////////////////////////	
			if($status==1){
				$where['current_price_status']=1;
				$list=Db::name('spaceOrder')->where($where)->where($wheret)->where($condition)->paginate(5);
				//echo db('spaceOrder')->getLastSql();
				$list_count=Db::name('spaceOrder')->where($where)->where($wheret)->where($condition)->select();	
			}
			elseif($status==2){
				$list=Db::name('spaceOrder')->where($where)->where($wheret)->where($condition)->paginate(5);
				//echo db('spaceOrder')->getLastSql();
				$list_count=Db::name('spaceOrder')->where($where)->where($wheret)->where($condition)->select();
			}else{
			
				$where['current_price_status']=0;
				$list=Db::name('spaceOrder')->where($where)->where($wheret)->where($condition)->paginate(5);
				//echo db('spaceOrder')->getLastSql();
				$list_count=Db::name('spaceOrder')->where($where)->where($wheret)->where($condition)->select();
			
			}

			// echo session('customer_name');
			// if(empty($_GET['page'])){

			//   session('customer_name',null);
			// }else{
			// 	echo 22222555;
			// }



			$list_count=json_decode(json_encode($list_count),true);
			
			$this->assign('count',count($list_count));
		$page=$list->render();
		$this->assign("lease_status",$lease_status);
		$this->assign("page", $page);
		$this->assign('cusname',$customer_name);
		$this->assign('spacname',$space_name);
		$this->assign('salesman',$salesname);
		$this->assign('stime',$stime);
		$this->assign('commpany_name',$commpany_name);
		$this->assign('etime',$etime);		
		$this->assign("list", $list);
	    $this->assign("status",$status);
		return $this->fetch();
	}



	/*园区添加*/		
	public function addOrder(){
		$wherec=array();
		// dump($_POST);
		// exit;
		if($_POST){
			// 	var_dump($_POST);
			// exit;

			$datas   = $this->request->param();
			$data['pic'] = $this->request->param('pic','');
			$data['space_type'] = $rent['space_type'] = $this->request->param('space_type',0);
			$data['space_id'] = $rent['space_id'] = $this->request->param('space_id',0);
			$data['commpany_name'] = $this->request->param('commpany_name','暂无');
			//var_dump($data);
			//exit;
			$data['incubator_id']  = $rent['incubator_id'] = $this->request->param('incubator_id',0);
			$data['start_time']= $stime = $this->request->param('stime','');
			$data['end_time']= $etime = $this->request->param('etime','');
			$data['payway_id']= $this->request->param('payway_id');
			$data['amount']=$rent['amount']=$this->request->param('amount',1);
			$data['order_num']=$this->request->param('order_num');
			// if(!empty($data['start_time'])){

			// }
			$stime= $data['start_time'];
			$etime= $data['end_time'];
			$data['stime']=strtotime($stime);
			$data['etime']=strtotime($etime);
			$dis=ceil(between_days($stime,$etime));
			// echo $stime;
			// echo $etime;
			// echo $dis;
			// exit;
			$months=ceil($dis/31);
			/*测试支付方式 1年2半年3季度4月5次*///2017/9/12 
			//$data['payway_id']=4;
			/*测试数据*/	
			//$data['payway_id']=1;
			$year_dayes=366;
			if($data['payway_id']==1){
				$year_num=ceil($dis/$year_dayes);
				//echo $year_num;
				// exit;
				for($i=0;$i<$year_num;$i++){
					$s=$i;
					$e=$i+1;
					$rent_cycle[]=date("Y-m-d",strtotime("+$s year",strtotime($stime))).'---'.date("Y-m-d",strtotime("+$e year",strtotime($stime)));
				}	
			}
			/*测试数据*/	
			$half_year_dayes=185;
			if($data['payway_id']==2){
				$year_num=ceil($dis/185);
				for($i=0;$i<$year_num;$i++){
					$s=$i*6;
					$e=($i+1)*6;

					$rent_cycle[]=date("Y-m-d",strtotime("+$s month",strtotime($stime))).'---'.date("Y-m-d",strtotime("+$e month",strtotime($stime)));
				}	
			}
			// dump($rent_cycle);
			// exit;
			// var_dump($rent);
			// exit;
			/*测试数据*/	
			$half_year_dayes=92;
			if($data['payway_id']==3){
				$year_num=ceil($dis/92);
				for($i=0;$i<$year_num;$i++){
					$s=$i*3;
					$e=($i+1)*3;

					$rent_cycle[]=date("Y-m-d",strtotime("+$s month",strtotime($stime))).'---'.date("Y-m-d",strtotime("+$e month",strtotime($stime)));
				}	
			}
			// var_dump($rent);
			// exit;
			/*测试数据*/	
			// echo $dis;
			// exit;
			$half_year_dayes=31;
			if($data['payway_id']==4){
				$year_num=ceil($dis/31);
				for($i=0;$i<$year_num;$i++){
					$s=$i;
					$e=$i+1;

					$rent_cycle[]=date("Y-m-d",strtotime("+$s month",strtotime($stime))).'---'.date("Y-m-d",strtotime("+$e month",strtotime($stime)));
				}	
			}
			// dump($rent_cycle);
			// exit;
			/*场地按照 次  场地有开放时间*/
			if($data['payway_id']==5){
				$rent_cycle[]=$stime.'---'.$etime;
			}
			// var_dump($rent);
			// exit;
			$data['space_type']= $this->request->param('space_type');
			/*测试数据 1空间2场地*/
			//$data['space_type']=1;
			//$data['lease_time']=$this->request->param('lease_time','');
			//var_dump(($data));
			//exit;
			if($data['space_type']==1){
				$station=Db::name('station')->where('id',$data['space_id'])->field('price,type,covered_area')->find();
				
				//var_dump($station);	

				$data['price']=$rent['price']=$station['price']?$station['price']:1;
				/*type1fangjian 2gongwei */
				if($station['type']==1){ 
					// $data['sum_price']=$rent['sum_price']=$station['price']*$station['covered_area']*$dis;	
					if($data['payway_id']==1){
						/*房间一天的价格*/
						$day_price=$station['price']*$station['covered_area'];	
						/*房间一周期的价格*/
						$rent['round_price'] =	$day_price*365*$data['amount'];
					}
					if($data['payway_id']==2){
						/*房间一天的价格*/
						$day_price=$station['price']*$station['covered_area'];	
						/*房间一周期的价格*/
						$rent['round_price'] =	$day_price*round(365/2,2)*$data['amount'];
					}
					if($data['payway_id']==3){
						/*房间一天的价格*/
						$day_price=$station['price']*$station['covered_area'];	
						/*房间一周期的价格*/
						$rent['round_price'] =	$day_price*round(365/4,2)*$data['amount'];
					}
					if($data['payway_id']==4){
						/*房间一天的价格*/
						$day_price=$station['price']*$station['covered_area'];	
						/*房间一周期的价格*/
						$rent['round_price'] =	$day_price*round(365/12,2)*$data['amount'];
					}
					// echo $rent['round_price'];
					// exit;

				}else{
					// $data['sum_price']=$rent['sum_price']=$station['price']*$months*$data['amount'];	
					if($data['payway_id']==1){
						/*房间一天的价格*/
						$price=$station['price'];	
						/*房间一周期的价格*/
						$rent['round_price'] =	$price*12*$data['amount'];
					}

					if($data['payway_id']==2){
						/*房间一天的价格*/
						$price=$station['price'];	
						/*房间一周期的价格*/
						$rent['round_price'] =	$price*6*$data['amount'];
					}
					if($data['payway_id']==3){
						/*房间一天的价格*/
						$price=$station['price'];	
						/*房间一周期的价格*/
						$rent['round_price'] =	$price*3*$data['amount'];
					}
					if($data['payway_id']==4){
						/*房间一天的价格*/
						$price=$station['price'];	
						/*房间一周期的价格*/
						$rent['round_price'] =	$price*$data['amount'];
					}
				}
				
				
				// echo $data['sum_price'];
				// exit;
				// var_dump($station);
				// exit;
			}else{
				$station=Db::name('site')->where('id',$data['space_id'])->field('price')->find();
				$data['price']=$rent['price']=$station['price']?$station['price']:1;
				/*场地价格是按照次 一次定一次*/
				// $data['sum_price']=$rent['sum_price']=$station['price'];
				$rent['round_price']=Db::name('site')->where('id',$data['space_id'])->value('price');
				$rent['round_price']=$rent['round_price']*$data['amount'];
			}
			// $rent['sum_price']=$data['sum_price'];
			$data['customer_name'] =$rent['customer_name']= $this->request->param('customer_name','');
			$data['space_name'] =$rent['space_name']= $this->request->param('space_name','');
			$data['customer_id'] = $rent['customer_id'] = $this->request->param('customer_id',0);
			$data['lease_status'] = $this->request->param('lease_status',0);
			$data['mobile']=$this->request->param('mobile',0);
			$data['service_price']=$this->request->param('service_price',0);
			$data['deposit_price']=$this->request->param('deposit_price',0);
			$data['is_receipt']=$this->request->param('is_receipt',0);
			$data['payway_id']=$rent['payway_id']=$this->request->param('payway_id',0);
			$salesman=$this->request->param('salesman',0);
			$data['salesman'] = get_user_name($salesman);
			$data['sales_id'] = $salesman;
			// $data['incubator_id']=db('user')->where('id',$salesman)->value('incubator_id');
			//$data['salesman']=$this->request->param('salesman');
			if(!empty($datas['file_urls']) && !empty($datas['file_names'])){
				$data['file']=$datas['file_urls'];
				$data['file']=implode(',', $data['file']);
				$data['file_names']=$datas['file_names'];
				$data['file_names']=implode(',', $data['file_names']);
			}
			
		    $data['create_time']=date("Y-m-d H:i:s");
		    $data['sum_price']='';
		    foreach ($rent_cycle as $key => $value) {
				$data['sum_price']+=$rent['round_price'];
			}
			$rent['sum_price']=$data['sum_price'];	
		    // var_dump($data);
		    // exit;
			$add=Db::name('spaceOrder')->insert($data);
			$rent['create_time']=date("Y-m-d H:i:s");
			$rent['order_id']=Db::name('spaceOrder')->getLastInsID();
			foreach ($rent_cycle as $key => $value) {
				$rent['cycle_time']=$value;
				$times=explode('---', $value);
				// var_dump($times);
				// exit;
				$rent['stime']=strtotime($times[0]);
				$rent['etime']=strtotime($times[1]);
				Db::name('rent')->insert($rent);
			}

			// $add=Db::name('spaceOrder')->insert($data);
			//var_dump($data);
			//exit;
			if($add){
			 if($data['space_type']==1){
			 	$dataspace=array('is_rent'=>1,'add_time'=>date("Y-m-d H:i:s"));
			 	//echo '##'.$data['space_id'].'&&&';
			 	db('station')->where('id',$data['space_id'])->update($dataspace);
			 }else{
			 	$dataspace=array('is_rent'=>1,'add_time'=>date("Y-m-d H:i:s"));
			 	db('site')->where('id',$data['space_id'])->update($dataspace);
			 }
			 $this->success('添加成功','Order/index');
			}else{
				$this->error('添加失败');
			}	

		}else{
			$where=array();	
			if(session('incubator_id')){
				$where['incubator_id']=session('incubator_id');
				$wherec['incubator_id']=session('incubator_id');
			}

			if(session('role_id')==3){
				$where['u.id']=session('ADMIN_ID');
				$wherec['sales_id']=session('ADMIN_ID');
				$this->assign('role',3);

			}else{
				$this->assign('role',500);
			}
			//echo session('name');
			//$this->assign('name',session('name'));
			$salesman=db('user')->alias('u')->join('roleUser r','u.id=r.user_id')->where('role_id',3)->where($where)->field('u.id,u.user_login')->select();
			// echo db('user')->getLastSql();
			// exit;
			$customers=db('customer')->where(array('status'=>1))->field('id,customer_name')->where($wherec)->select();
			//dump($customers);
			$namess=session('name');
			// echo  $namess;
			// exit;
			$this->assign('namess',$namess);
			$this->assign('salesman',$salesman);
			$this->assign('customers',$customers);
			return $this->fetch();
		}





	}
	function staging($type,$stime,$etime,$monthmoney){
		$stime='2017-09-12';
		$etime='2018-10-12';
		$dis=between_days($stime,$etime);
		$a=0;
			// echo   date("Y-m-d",strtotime("+$a year",strtotime($stime)));
			// exit;
			// echo $stime+1;
			// exit;
			// echo $dis;
			// exit;
			// $distance=$etime-$stime;
			// echo $distance;
			// exit;
		$type=1;	

		if($type==1){
			$year_num=ceil($dis/365);
			for($i=0;$i<$year_num;$i++){
				// echo date("Y-m-d",strtotime("+$i year",strtotime($stime)));
				// exit;
				$s=$i;
				$e=$i+1;
				$rent[$i]['cycle']=date("Y-m-d",strtotime("+$s year",strtotime($stime))).'---'.date("Y-m-d",strtotime("+$e year",strtotime($stime)));
					// echo $rent[$i]['cycle'];
					// exit;

			}	
		}
	}
	/*详情*/
	public function detailOrder(){
		$wherec=array();
		$post=$this->request->param();
		$where=array();
		$id= $post['id'];
		if($id){
			$where['id']= $id;
		}else{
			return info_back('','id不能为空',false);
		}
		$detail= Db::name('spaceOrder')->where($where)->find();
		$url=explode(',', $detail['file']);
		$name=explode(',',$detail['file_names']);
		$arr=array();
		for($i=0; $i<count($url);$i++){
			$arr['url']=$url[$i];
			$arr['name']=$name[$i];
			$arrs[]=$arr;
		}
		//dump($arrs);
		// 	 dump($detail);
		// 	 exit;

	
		$where=array();	
		if(session('incubator_id')){
			$where['incubator_id']=session('incubator_id');
			$wherec['incubator_id']=session('incubator_id');
		}

		if(session('role_id')==3){
			$where['u.id']=session('ADMIN_ID');
			$wherec['sales_id']=session('ADMIN_ID');
		}
		$salesman=db('user')->alias('u')->join('roleUser r','u.id=r.user_id')->where('role_id',3)->where($where)->field('u.id,u.user_login')->select();
		$customers=db('customer')->where(array('status'=>1))->where($wherec)->field('id,customer_name')->select();
		$this->assign('salesman',$salesman);
	
		//dump($customers);
		$this->assign('arrs',$arrs);
		$this->assign('customers',$customers);
		$this->assign('detail',$detail);
		return $this->fetch();
	}

	/*编辑园区*/	
	public function editOrder(){
		// var_dump($_POST);
		// exit;
		$wherec=array();
		$post=$this->request->param();

		if($_POST){
			// var_dump($_POST);
			// exit;	
			$datas   = $this->request->param();
			$data['pic'] = $this->request->param('pic','');
			$data['commpany_name'] = $this->request->param('commpany_name','暂无');
			$data['space_type'] = $rent['space_type'] = $this->request->param('space_type',0);
			$data['status']  = $this->request->param('status',1);
			$data['space_id'] = $rent['space_id'] = $this->request->param('space_id',0);
			$data['incubator_id']  = $rent['incubator_id'] = $this->request->param('incubator_id',0);
			$data['start_time']= $stime = $this->request->param('stime','');
			$data['end_time']= $etime = $this->request->param('etime','');
			$data['payway_id']=$rent['payway_id']= $this->request->param('payway_id');
			$data['amount']=$rent['amount']=$this->request->param('amount',1);
			$data['order_num']=$this->request->param('order_num');
			$stime= $data['start_time'];
			$etime= $data['end_time'];
			$rent['stime']=strtotime($data['start_time']);;
			$rent['etime']=strtotime($data['end_time']);
			if(session('incubator_id')){
				$rent['incubator_id']=session('incubator_id');
			}	
			// echo $stime;;
			// echo $etime;;
			// echo between_days($stime,$etime);
			// exit;
			$dis=ceil(between_days($stime,$etime));
			// echo $stime;
			// echo $etime;
			// echo $dis;
			// exit;
			$months=ceil($dis/31);
			/*测试支付方式 1年2半年3季度4月5次*///2017/9/12 
			//$data['payway_id']=4;
			/*测试数据*/	
			//$data['payway_id']=1;
			$year_dayes=366;
			if($data['payway_id']==1){
				$year_num=ceil($dis/$year_dayes);
				//echo $year_num;
				// exit;
				for($i=0;$i<$year_num;$i++){
					$s=$i;
					$e=$i+1;
					$rent_cycle[]=date("Y-m-d",strtotime("+$s year",strtotime($stime))).'---'.date("Y-m-d",strtotime("+$e year",strtotime($stime)));
				}	
			}
			/*测试数据*/	
			$half_year_dayes=185;
			if($data['payway_id']==2){
				$year_num=ceil($dis/185);
				for($i=0;$i<$year_num;$i++){
					$s=$i*6;
					$e=($i+1)*6;

					$rent_cycle[]=date("Y-m-d",strtotime("+$s month",strtotime($stime))).'---'.date("Y-m-d",strtotime("+$e month",strtotime($stime)));
				}	
			}
			// dump($rent_cycle);
			// exit;
			// var_dump($rent);
			// exit;
			/*测试数据*/	
			$half_year_dayes=92;
			if($data['payway_id']==3){
				$year_num=ceil($dis/92);
				for($i=0;$i<$year_num;$i++){
					$s=$i*3;
					$e=($i+1)*3;

					$rent_cycle[]=date("Y-m-d",strtotime("+$s month",strtotime($stime))).'---'.date("Y-m-d",strtotime("+$e month",strtotime($stime)));
				}	
			}
			// var_dump($rent);
			// exit;
			/*测试数据*/	
			// echo $dis;
			// exit;
			$half_year_dayes=31;
			if($data['payway_id']==4){
				$year_num=ceil($dis/31);
				for($i=0;$i<$year_num;$i++){
					$s=$i;
					$e=$i+1;

					$rent_cycle[]=date("Y-m-d",strtotime("+$s month",strtotime($stime))).'---'.date("Y-m-d",strtotime("+$e month",strtotime($stime)));
				}	
			}
			// echo $stime;
			// echo $data['payway_id'];
			// exit;
			// dump($rent_cycle);
			// exit;
			/*场地按照 次  场地有开放时间*/
			if($data['payway_id']==5){
				$rent_cycle[]=$stime.'---'.$etime;
			}
			// var_dump($rent);
			// exit;
			$data['space_type']= $this->request->param('space_type');
			/*测试数据 1空间2场地*/
			//$data['space_type']=1;
			//$data['lease_time']=$this->request->param('lease_time','');
			// echo $data['space_type'];
			// exit;
			if($data['space_type']==1){
				$station=Db::name('station')->where('id',$data['space_id'])->field('price,type,covered_area')->find();
				


				$data['price']=$rent['price']=$station['price']?$station['price']:1;
				/*type1fangjian 2gongwei */
				if($station['type']==1){ 
					//$data['sum_price']=$rent['sum_price']=$station['price']*$station['covered_area']*$dis;	
					if($data['payway_id']==1){
						/*房间一天的价格*/
						$day_price=$station['price']*$station['covered_area'];	
						/*房间一周期的价格*/
						$rent['round_price'] =	$day_price*365*$data['amount'];
					}
					if($data['payway_id']==2){
						/*房间一天的价格*/
						$day_price=$station['price']*$station['covered_area'];	
						/*房间一周期的价格*/
						$rent['round_price'] =	$day_price*round(365/2,2)*$data['amount'];
					}
					if($data['payway_id']==3){
						/*房间一天的价格*/
						$day_price=$station['price']*$station['covered_area'];	
						/*房间一周期的价格*/
						$rent['round_price'] =	$day_price*round(365/4,2)*$data['amount'];
					}
					if($data['payway_id']==4){
						/*房间一天的价格*/
						$day_price=$station['price']*$station['covered_area'];	
						/*房间一周期的价格*/
						$rent['round_price'] =	$day_price*round(365/12,2)*$data['amount'];
					}
					// echo $rent['round_price'];
					// exit;

				}else{
					//$data['sum_price']=$rent['sum_price']=$station['price']*$months*$data['amount'];	
					if($data['payway_id']==1){
						/*房间一天的价格*/
						$price=$station['price'];	
						/*房间一周期的价格*/
						$rent['round_price'] =	$price*12*$data['amount'];
					}

					if($data['payway_id']==2){
						/*房间一天的价格*/
						$price=$station['price'];	
						/*房间一周期的价格*/
						$rent['round_price'] =	$price*6*$data['amount'];
					}
					if($data['payway_id']==3){
						/*房间一天的价格*/
						$price=$station['price'];	
						/*房间一周期的价格*/
						$rent['round_price'] =	$price*3*$data['amount'];
					}
					if($data['payway_id']==4){
						/*房间一天的价格*/
						$price=$station['price'];	
						/*房间一周期的价格*/
						$rent['round_price'] =	$price*$data['amount'];
					}
				}
				
				
				// echo $data['sum_price'];
				// exit;
				// var_dump($station);
				// exit;
			}else{
				// echo 22222;
				// exit;
				/*场地价格是按照次 一次定一次*/
				$station=Db::name('site')->where('id',$data['space_id'])->field('price')->find();
				$data['price']=$rent['price']=$station['price']?$station['price']:1;
				//$data['sum_price']=$rent['sum_price']=$station['price'];
				$rent['round_price']=Db::name('site')->where('id',$data['space_id'])->value('price');
				$rent['round_price']=$rent['round_price']*$data['amount'];
			}
			//$rent['sum_price']=$data['sum_price'];
			//$data['space_id'] =$rent['space_id']= $this->request->param('space_id','');
			$data['customer_name'] =$rent['customer_name']= $this->request->param('customer_name','');
			$data['space_name'] =$rent['space_name']= $this->request->param('space_name','');
			$data['customer_id'] = $rent['customer_id'] = $this->request->param('customer_id',0);
			$data['lease_status'] = $this->request->param('lease_status',0);
			$data['mobile']=$this->request->param('mobile',0);
			// dump($_POST);
			//  exit;
			// $data['pic']=$datas['photo_urls'];
		
			// $data['pic_names']=$datas['photo_names'];
			
			// $data['pic']=implode(',', $data['pic']);
			// $data['pic_names']=implode(',', $data['pic_names']);
			
			$data['service_price']=$this->request->param('service_price',0);
			$data['deposit_price']=$this->request->param('deposit_price',0);
			$data['id']=$rent['order_id']=$_POST['id'];
			$data['is_receipt']=$this->request->param('is_receipt',0);
			//$data['sum_price']=$this->request->param('sum_price',0);
			$data['payway_id']=$this->request->param('payway_id',0);
			// dump($_POST);
			// exit;
			//$data['lease_time']=$this->request->param('lease_time','');
			//$data['salesman']=$this->request->param('salesman');
			$salesman=$this->request->param('salesman',0);
			$data['salesman'] = get_user_name($salesman);
			$data['sales_id'] = $salesman;
			if(!empty($datas['file_urls']) && !empty($datas['file_names'])){
				$data['file']=$datas['file_urls'];
				$data['file']=implode(',', $data['file']);
				$data['file_names']=$datas['file_names'];
				$data['file_names']=implode(',', $data['file_names']);
			}
			
			$rent['create_time']=date("Y-m-d H:i:s");
		 //    	dump($_POST);
			// exit;
			$order_rent=db('rent')->where('order_id',$data['id'])->find();
			if($order_rent){
				db('rent')->where('order_id',$data['id'])->where('status',1)->delete();
			}
			$data['sum_price']='';
		    foreach ($rent_cycle as $key => $value) {
				$data['sum_price']+=$rent['round_price'];
			}
			$rent['sum_price']=$data['sum_price'];	
			// var_dump($rent_cycle);
			// exit;
			foreach ($rent_cycle as $key => $value) {
				$rent['cycle_time']=$value;
				$times=explode('---', $value);
				// var_dump($times);
				// exit;
				$rent['stime']=strtotime($times[0]);
				$rent['etime']=strtotime($times[1]);
				Db::name('rent')->insert($rent);
			}
			$data['create_time']=date("Y-m-d H:i:s");
			if(session('incubator_id')){
				$data['incubator_id']=session('incubator_id');
				
			}
			$add=Db::name('spaceOrder')->update($data);
			// dump($data);
			// exit;
			if($add){

				if($data['space_type']==1){
					db('station')->where('id',$_POST['room_id'])->setField('is_rent',0);
					$dataspace=array('is_rent'=>1,'add_time'=>date("Y-m-d H:i:s"));
					db('station')->where('id',$data['space_id'])->update($dataspace);
				}else{
					db('site')->where('id',$_POST['room_id'])->setField('is_rent',0);
					$dataspace=array('is_rent'=>1,'add_time'=>date("Y-m-d H:i:s"));
					db('site')->where('id',$data['space_id'])->update($dataspace);
				}
			 //$this->success('添加成功','Order/index');
			 $this->success('编辑成功','Order/index');
			}else{
				$this->error('编辑失败');
			}	

		}
		$where=array();
		$id= $post['id'];
		if($id){
			$where['id']= $id;
		}else{
			return info_back('','id不能为空',false);
		}
		$detail= Db::name('spaceOrder')->where($where)->find();
		$url=explode(',', $detail['file']);
		$name=explode(',',$detail['file_names']);
		$arr=array();
		for($i=0; $i<count($url);$i++){
			$arr['url']=$url[$i];
			$arr['name']=$name[$i];
			$arrs[]=$arr;
		}


		
		$where=array();	
		if(session('incubator_id')){
			$where['incubator_id']=session('incubator_id');
			$wherec['incubator_id']=session('incubator_id');
		}

		// if(session('role_id')==3){
		// 	$where['u.id']=session('ADMIN_ID');
		// 	$wherec['sales_id']=session('ADMIN_ID');
		// }
			if(session('role_id')==3){
				$where['u.id']=session('ADMIN_ID');
				$wherec['sales_id']=session('ADMIN_ID');
				$this->assign('role',3);

			}else{
				$this->assign('role',500);
			}
		$salesman=db('user')->alias('u')->join('roleUser r','u.id=r.user_id')->where('role_id',3)->where($where)->field('u.id,u.user_login')->select();
		$customers=db('customer')->where(array('status'=>1))->where($wherec)->field('id,customer_name')->select();
		$namess=session('name');
		$this->assign('namess',$namess);
		$this->assign('salesman',$salesman);
		//dump($customers);
		$this->assign('arrs',$arrs);
		$this->assign('customers',$customers);
		$this->assign('detail',$detail);
		return $this->fetch();

	}


	/*删除园区*/
	public function delOrder(){
		$data['id']= $this->request->param('id',0,'intval');

		$data['status']=0;
	    $info_ids=db('spaceOrder')->where('id',$data['id'])->field('space_type,space_id')->find();	
		$del=Db::name('spaceOrder')->update($data);

		//$del=Db::name('spaceOrder')->where('id',$data['id'])->delete();	
		if($del){
			 
    		
				if($info_ids['space_type']==1){
				
					db('station')->where('id',$info_ids['space_id'])->setField('is_rent',0);
				
				}else{
					db('site')->where('id',$info_ids['space_id'])->setField('is_rent',0);
				
				}
			db('rent')->where('order_id',$data['id'])->delete();
			return info_back('','删除成功',true);
		}else{
			return info_back('','删除失败',false);
		}

	}	

    function lostOrder(){
    	$param=$this->request->param();
    	if($_POST){
    		$_POST['lease_status']=1;
    		$_POST['lost_time']=date("Y-m-d");
    		$save=db('spaceOrder')->update($_POST);
    		db('rent')->where('order_id',$_POST['id'])->where('status',1)->delete();
    		if($save){
    			$info_ids=db('spaceOrder')->where('id',$_POST['id'])->field('space_type,space_id')->find();
    			// var_dump($info_ids);
    			// exit;
				if($info_ids['space_type']==1){
					db('station')->where('id',$info_ids['space_id'])->setField('is_rent',0);
				
				}else{
					db('site')->where('id',$info_ids['space_id'])->setField('is_rent',0);
				
				}
    			$this->success('退租成功','Order/index');
    		}
    	}
    	$this->assign('id',$param['id']);
    	return $this->fetch();
    } 

	/*成交信息*/
	function detailTotal(){

		/*押金总额*/
		$data['total_deposit']=db('spaceOrder')->sum('deposit_price');
		$data['total_rent']=db('rent')->where(array('status'=>1))->sum('round_price');
		$data['total_customer']=db('customer')->count();
		$data['total_order']=db('spaceOrder')->count();
		$data['total_order_price']=db('spaceOrder')->sum('sum_price');
		$where=array('r.space_type'=>1,'s.type'=>2);
		$data['tptal_order_station']=db('spaceOrder')->alias('r')->where($where)->join('station s','r.space_id=s.id')->count();
		$wheres=array('r.space_type'=>1,'s.type'=>1);
		$data['tptal_order_room']=db('spaceOrder')->alias('r')->where($wheres)->join('station s','r.space_id=s.id')->count();
		echo $data['tptal_order_station'];
		exit;
		/*总成交面积 面积--房间*/

		$where=array('r.space_type'=>1);
		$space_info=db('spaceOrder')->alias('r')->where($where)->join('station s','r.space_id=s.id')->select();
		$data['customer_num']=db('customer')->count();
	    	// echo $customer_info;
	    	// exit;
		$data['order_num']=count($space_info);


		echo "<pre>"; print_r($space_info); echo "</pre>";



















	}


	function get_space(){
		// var_dump($_POST);
		// exit;
		$where=array();
		if(session('incubator_id')){
			$where['incubator_id']=session('incubator_id');
		}
		$edit= $this->request->param('edit');
		$type=$_POST['type'];
		if($type==1){
			$list=db("station")->where(array('status'=>1,'is_rent'=>0))->field('id,station_name,incubator_id')->where($where)->select();
			if($edit){

				$edit_space=db("station")->where(array('id'=>$_POST['space_id']))->where($where)->field('id,station_name,incubator_id')->find();
				// var_dump($edit_space);
				// exit;
				$list[count($list)]=$edit_space;
			}
		}else{
			$list=db("site")->where(array('status'=>1,'is_rent'=>0))->where($where)->field('id,site_name,incubator_id')->select();
			if($edit){

				$edit_space=db("site")->where(array('id'=>$_POST['space_id']))->where($where)->field('id,site_name,incubator_id')->find();
				// var_dump($edit_space);
				// exit;
				$list[count($list)]=$edit_space;
			}
		}
		return json($list);
	}

	function backDeposit(){
		$id= $this->request->param('id',0,'intval');
		if($id){
			$result=db('spaceOrder')->where(array('id'=>$id))->setField('deposit_price','0');
			if($result){
				return 1;
			}else{
				return 0;
			}
		}
	}	

	function tip(){
		$where=array();
		$arr=array();
		$str = $_POST['oData']; 
		if(session('role_id')==3){
			$where['sales_id']=session('ADMIN_ID');
		}
		// var_dump($str);
		// exit; 
//$str = "b";  
		//$str_array = array("b","boy11","boy2", "boy22", "baya33", "boy3", "boy4", "bay44", "boy2323", "bay213"); 
		if(session('incubator_id')){
			$where['incubator_id']=session('incubator_id');
		} 
		$where['status']=1;
		$str_array=db('customer')->where('customer_name','like','%'.$str.'%')->where($where)->select();
		// echo db('customer'->getLastSql();
		// 	exit;
		foreach ($str_array as $key => $value) {
			$arr[]=$value['customer_name'];
		}
		// echo db('customer')->getLastSql();
		// var_dump($str_array);
		// exit;
		// $str1 = "";  
		$str1=implode(',', $arr);
		// $Counts = count($str_array);  
		// for($i=0;$i<$Counts;$i++){  
  //   //  
		// 	if(preg_match("/^$str/",$str_array[$i])){  
		// 		$str1 .= $str_array[$i];  
		// 		if($i != $Counts-1)  
		// 			$str1 .= ",";  
		// 	}  
		// }  
//echo $str2;  
		echo $str1;  
	}
	function tips(){
		$where=array();
		$arr=array();
		$str = $_POST['oData']; 
		if(session('role_id')==3){
			$where['sales_id']=session('ADMIN_ID');
		}
		// var_dump($str);
		// exit; 
//$str = "b";  
		//$str_array = array("b","boy11","boy2", "boy22", "baya33", "boy3", "boy4", "bay44", "boy2323", "bay213"); 
		if(session('incubator_id')){
			$where['incubator_id']=session('incubator_id');
		} 
		$where['status']=1;
		$str_array=db('customer')->where('customer_name','like','%'.$str.'%')->where($where)->select();
		// echo db('customer'->getLastSql();
		// 	exit;
		foreach ($str_array as $key => $value) {
			$arr[]=$value['customer_name'];
		}
		// echo db('customer')->getLastSql();
		// var_dump($str_array);
		// exit;
		// $str1 = "";  
		$str1=implode(',', $arr);
		// $Counts = count($str_array);  
		// for($i=0;$i<$Counts;$i++){  
  //   //  
		// 	if(preg_match("/^$str/",$str_array[$i])){  
		// 		$str1 .= $str_array[$i];  
		// 		if($i != $Counts-1)  
		// 			$str1 .= ",";  
		// 	}  
		// }  
//echo $str2;  
		echo $str1;  
	}
	function salesTip(){
		//var_dump($_POST);
		// exit;
		$arr=array();
		$where=array();
		if(session('incubator_id')){
			$where['incubator_id']=session('incubator_id');
		} 
		$where['user_status']=1;
		$str = $_POST['oData']; 
		$type=$this->request->param('room_type',0,'intval');
		$id=$this->request->param('room_id',0,'intval');
		if($type && $id){
			// $type = $_POST['room_type']; 
			// $id = $_POST['room_id']; 
		// var_dump($_POST);
		// exit;
			if($type==1){
				$where['incubator_id']=db('station')->where('id',$id)->value("incubator_id");
			}else{
				$where['incubator_id']=db('site')->where('id',$id)->value("incubator_id");
			}
		}
		// $type = $_POST['room_type']; 
		// $id = $_POST['room_id']; 
		// // var_dump($_POST);
		// // exit;
		// if($type==1){
		// 	$where['incubator_id']=db('station')->where('id',$id)->value("incubator_id");
		// }else{
		// 	$where['incubator_id']=db('site')->where('id',$id)->value("incubator_id");
		// }
		$str_array=db('user')->alias('u')->join('roleUser r','u.id=r.user_id')->where($where)->where('role_id',3)->where('user_login','like','%'.$str.'%')->field('u.id,u.user_login')->select();
		//echo db('user')->getLastSql();

		foreach ($str_array as $key => $value) {
			$arr[]=$value['user_login'];
		}
		$str1=implode(',', $arr);
		echo $str1;

	}
	function salesTips(){
		//var_dump($_POST);
		// exit;
		$arr=array();
		$where=array();
		if(session('incubator_id')){
			$where['incubator_id']=session('incubator_id');
		} 
		$where['user_status']=1;
		$str = $_POST['oData']; 
		$type=$this->request->param('room_type',0,'intval');
		$id=$this->request->param('room_id',0,'intval');
		if($type && $id){
			// $type = $_POST['room_type']; 
			// $id = $_POST['room_id']; 
		// var_dump($_POST);
		// exit;
			if($type==1){
				$where['incubator_id']=db('station')->where('id',$id)->value("incubator_id");
			}else{
				$where['incubator_id']=db('site')->where('id',$id)->value("incubator_id");
			}
		}
		// $type = $_POST['room_type']; 
		// $id = $_POST['room_id']; 
		// // var_dump($_POST);
		// // exit;
		// if($type==1){
		// 	$where['incubator_id']=db('station')->where('id',$id)->value("incubator_id");
		// }else{
		// 	$where['incubator_id']=db('site')->where('id',$id)->value("incubator_id");
		// }
		$str_array=db('user')->alias('u')->join('roleUser r','u.id=r.user_id')->where($where)->where('role_id',3)->where('user_login','like','%'.$str.'%')->field('u.id,u.user_login')->select();
		//echo db('user')->getLastSql();

		foreach ($str_array as $key => $value) {
			$arr[]=$value['user_login'];
		}
		$str1=implode(',', $arr);
		echo $str1;

	}
	function getCustomerId(){
		$arr=array();
		$str = $_POST['customer_name'];
		$id=get_customer_id($str);
		echo $id; 
	}
	function getSalesId(){
		$arr=array();
		$str = $_POST['snames'];
		$id=db('user')->where('user_login',$str)->value('id');
		echo $id; 
	}


















}